/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.base.tools;

import tk.mybatis.mapper.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.base.tools
 * @author:LiBo/Alex
 * @create-date:2020-07-18 16:37
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class TokenUtils {

    static int ignoreTg = 0;

    static int startTg = 1;

    static int readTg = 2;

    /**
     * 获取字符串里的占位符列表, 占位符样式 ${param}
     * @param content
     * @return
     */
    public static List<String> getPlaceholderList(String content){
        char[] textChars = content.toCharArray();
        StringBuilder textSofar = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        // 存储占位符 位置信息集合
        List<String> placeholderList = new ArrayList<>();
        // 当前标识
        int modeTg = ignoreTg;
        for (int m = 0; m < textChars.length; m++) {
            char c = textChars[m];
            textSofar.append(c);
            switch (c) {
                case '$': {
                    modeTg = startTg;
                    sb.append(c);
                }
                break;
                case '{': {
                    if (modeTg == startTg) {
                        sb.append(c);
                        modeTg = readTg;
                    } else {
                        if (modeTg == readTg) {
                            sb = new StringBuilder();
                            modeTg = ignoreTg;
                        }
                    }
                }
                break;
                case '}': {
                    if (modeTg == readTg) {
                        modeTg = ignoreTg;
                        sb.append(c);
                        String str = sb.toString();
                        if (StringUtil.isNotEmpty(str)) {
                            placeholderList.add(str);
                            textSofar = new StringBuilder();
                        }
                        sb = new StringBuilder();
                    } else if (modeTg == startTg) {
                        modeTg = ignoreTg;
                        sb = new StringBuilder();
                    }
                }
                default: {
                    if (modeTg == readTg) {
                        sb.append(c);
                    } else if (modeTg == startTg) {
                        modeTg = ignoreTg;
                        sb = new StringBuilder();
                    }
                }
            }
        }
        return placeholderList;
    }
}
